﻿using ELTE.SEM.Data.Repository;
using ELTE.SEM.Data.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace ELTE.SEM.Data.Managers
{
    public class FolderManager : ManagerBase
    {
        FolderRepository Repo { get; set; }
        ErrorLogRepository ErrorLogRepo { get; set; }

        public FolderManager()
            : base() { 
            Repo = new FolderRepository(this.Entities);
            ErrorLogRepo = new ErrorLogRepository(this.Entities);
        }
        public FolderManager(SemDataEntities entities)
            : base(entities) { 
            Repo = new FolderRepository(entities);
            ErrorLogRepo = new ErrorLogRepository(entities);
        }

        public bool BulkInsert(List<SemDataViewModel> semData, string rootFolderPath)
        {
            try
            {
                var directories = semData.Where(data => data.IsDirectory).ToList();
                return Repo.AddFolders(Repo.GetRootFolderViewModel(directories, rootFolderPath));
            }
            catch (Exception ex)
            {
                ErrorLogRepo.AddErrorLog(MethodInfo.GetCurrentMethod().DeclaringType.Name, MethodInfo.GetCurrentMethod().Name,ex.ToString(), String.Empty);
                return false;
            }

        }

        public SemFolderViewModel GetFolderViewModel(SemDataViewModel dataViewModel)
        {
            SemFolderViewModel folderByFolderName = null;
            try
            {
                SemFolderViewModel rootFolderViewModel = Repo.GetFolders();
                folderByFolderName = GetFolderViewModelRecursive(rootFolderViewModel, dataViewModel);
            }
            catch (Exception ex)
            {
                ErrorLogRepo.AddErrorLog(MethodInfo.GetCurrentMethod().DeclaringType.Name, MethodInfo.GetCurrentMethod().Name, ex.ToString(), String.Empty);
            }
            
            return folderByFolderName;
        }

        private SemFolderViewModel GetFolderViewModelRecursive(SemFolderViewModel currentRoot, SemDataViewModel dataViewModel)
        {
            SemFolderViewModel folder = null;
            try
            {
                if (currentRoot != null)
                {
                    if (currentRoot.FolderName == dataViewModel.Name)
                    {
                        if (currentRoot.Parent != null && currentRoot.Parent.FolderName == dataViewModel.ParentFolder)
                        {
                            //ha az aktuálisan vizsgált SemFolderViewModel az az, amit keresünk, visszaadjuk azt
                            folder = currentRoot;
                        }
                    }
                    else
                    {
                        //ha az aktuális SemFolderViewModel nem a keresett, megvizsgáljuk a gyermekeit is
                        //már ha van neki
                        if (currentRoot.Children != null && currentRoot.Children.Count > 0)
                        {
                            foreach (SemFolderViewModel child in currentRoot.Children)
                            {
                                SemFolderViewModel folderGot = GetFolderViewModelRecursive(child, dataViewModel);
                                if (folderGot != null)
                                {
                                    folder = folderGot;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            //nincs gyermek
                            folder = null;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorLogRepo.AddErrorLog(MethodInfo.GetCurrentMethod().DeclaringType.Name, MethodInfo.GetCurrentMethod().Name, ex.ToString(), String.Empty);
            }

            return folder;
        }

        public string GetFolderFullPath(int folderId)
        {
            string result = string.Empty;
            try
            {
                result = Repo.GetById(folderId).FullPath;
            }
            catch (Exception ex)
            {
                ErrorLogRepo.AddErrorLog(MethodInfo.GetCurrentMethod().DeclaringType.Name, MethodInfo.GetCurrentMethod().Name, ex.ToString(), String.Empty);
            }
            return result;
        }

    }
}
